/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * test.java
 *
 * Created on 30 Jul 09, 11:02:12
 */

package amd.ui;

import amd.core.FileOperations;
import amd.core.UIListener;
import java.awt.event.KeyEvent;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

/**
 *
 * @author Latifah Dwi Nurachma
 */
public class FrameMenuUpload extends javax.swing.JInternalFrame {
    private String docPath;
    private String sourcePath;
    private UIListener listener;

    /** Creates new form test */
    public FrameMenuUpload(JFrame frame) throws Exception {
        sourcePath = "D:/t_peh's Data/Kuliah @ POLBAN/Semester 6/Tugas Akhir/Implementasi/sourcePath";
        listener = new UIListener(frame);
        initComponents();
        ((javax.swing.plaf.basic.BasicInternalFrameUI)this.getUI()).setNorthPane(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jTextFieldPath = new javax.swing.JTextField();
        jButtonBrowse = new javax.swing.JButton();
        jButtonCancel = new javax.swing.JButton();
        jButtonOK = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jTextFieldPath.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jTextFieldPathFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                jTextFieldPathFocusLost(evt);
            }
        });
        jTextFieldPath.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jTextFieldPathKeyPressed(evt);
            }
        });

        jButtonBrowse.setText("Browse");
        jButtonBrowse.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButtonBrowseMouseReleased(evt);
            }
        });

        jButtonCancel.setText("Cancel");
        jButtonCancel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButtonCancelMouseReleased(evt);
            }
        });

        jButtonOK.setText("OK");
        jButtonOK.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                jButtonOKMouseReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jTextFieldPath, javax.swing.GroupLayout.DEFAULT_SIZE, 266, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonBrowse))
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGap(113, 113, 113)
                        .addComponent(jButtonOK)
                        .addGap(42, 42, 42)
                        .addComponent(jButtonCancel)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextFieldPath, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonBrowse))
                .addGap(18, 18, 18)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonOK)
                    .addComponent(jButtonCancel))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(22, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(16, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jTextFieldPathFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldPathFocusGained
        //browseDocument();
}//GEN-LAST:event_jTextFieldPathFocusGained

    private void jTextFieldPathFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jTextFieldPathFocusLost
        docPath = jTextFieldPath.getText();
}//GEN-LAST:event_jTextFieldPathFocusLost

    private void jTextFieldPathKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTextFieldPathKeyPressed
        char c = (char) evt.getKeyCode();
        if (c == KeyEvent.VK_ENTER) {
            docPath = jTextFieldPath.getText();
            uploadDocument(docPath);
        }
}//GEN-LAST:event_jTextFieldPathKeyPressed

    private void jButtonBrowseMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonBrowseMouseReleased
        browseDocument();
}//GEN-LAST:event_jButtonBrowseMouseReleased

    private void jButtonCancelMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonCancelMouseReleased
        this.setVisible(false);
}//GEN-LAST:event_jButtonCancelMouseReleased

    private void jButtonOKMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonOKMouseReleased
        uploadDocument(docPath);
}//GEN-LAST:event_jButtonOKMouseReleased


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonBrowse;
    private javax.swing.JButton jButtonCancel;
    private javax.swing.JButton jButtonOK;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JTextField jTextFieldPath;
    // End of variables declaration//GEN-END:variables

    private void uploadDocument(String docPath) {
        listener.uploadDocument(docPath);
//        try {
//            FileOperations.copyFilePdf(docPath, sourcePath);
//            JOptionPane.showMessageDialog(this, "File " + new File(docPath).getName() + " telah berhasil di upload.", "Berhasil", JOptionPane.INFORMATION_MESSAGE, null);
//        } catch (Exception ex) {
//            JOptionPane.showMessageDialog(this,
//                    "Dokumen gagal di-upload\n"+ex.getMessage(),
//                    "Gagal", JOptionPane.INFORMATION_MESSAGE, null);
//        }
    }

    private void browseDocument() {
        //listener.
//        JFileChooser chooser = new JFileChooser();
//        //chooser.setCurrentDirectory(currentDir);
//        //chooser.setFileFilter(fileFilter);
//        chooser.setAcceptAllFileFilterUsed(false);
//        int result = chooser.showOpenDialog(this);
//        String fileName = "";
//        if (result == JFileChooser.APPROVE_OPTION) {
//            File file = chooser.getSelectedFile();
//            String name = chooser.getSelectedFile().getPath();
//            //currentDir = new File(name).getParentFile();
//            if(file.getName().endsWith(".pdf")){
//                fileName = file.getPath();
//            }
//            else{
//                //message box bahwa dia mengupload file bukan pdf
//                JOptionPane.showMessageDialog(this, "File "+ file.getName() + " tidak berformat PDF.\nFile tidak berhasil diupload.","Gagal",JOptionPane.WARNING_MESSAGE,null);
//            }
//        }
//        jTextFieldPath.setText(fileName);
    }


}
